home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_02_03
/
2n03050a
< prev
next >
Wrap
Text File
|
1991-01-14
|
11KB
|
401 lines
/********************************************************************
* BEZDIS.C -- Interactive Bezier Curve Display.
*
* Author : Bob Zigon
* Date : July 26, 1989
********************************************************************/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "bios.h"
#include "graphics.h"
#include "keys.h"
/*******************************************************************
* Global Defines
********************************************************************/
#define OFF 0
#define ON (!OFF)
#define MAXTDivisions 150
#define MINTDivisions 30
#define MAXCP 10
#define CPDx 0.05
#define CPDy 0.05
/*******************************************************************
* Variable Declarations
********************************************************************/
double XCp[MAXCP+1], /* X Control Points */
YCp[MAXCP+1]; /* Y Control Points */
short int XCpScr[MAXCP+1],
YCpScr[MAXCP+1];
double XCurve[MAXTDivisions+1], /* X Curve Points */
YCurve[MAXTDivisions+1]; /* Y Curve Points */
short int Xv[MAXTDivisions+1],
Yv[MAXTDivisions+1];
short int XvpMin, /* Viewport dimensions */
XvpMax,
YvpMin,
YvpMax,
XsMax, /* Screen Dimensions in pixels */
YsMax;
short int TDivisions = MINTDivisions,
NumCP,
CurCP = 0,
Refresh = ON;
short int CurveColor,
LineColor,
CrossColor,
BgdColor;
double XCpMin = 0.0,
XCpMax = 5.0,
YCpMin = 0.0,
YCpMax = 5.0;
char *HelpMsg[] =
{
" F1 - Help",
" F2 - Toggle refresh of CP line segments",
" ",
" Left - Move current CP left 1 unit",
" Right - Move current CP right 1 unit",
" Up - Move current CP up 1 unit",
" Down - Move current CP down 1 unit",
" ",
" PgUp - Next CP",
" PgDn - Prev CP",
" Ins - Insert CP",
" Del - Delete CP",
" ",
" Home - Add 5 to number of parametric divisions",
" End - Sub 5 from number of parametric divisions ",
" ",
" ESC - QUIT"
};
#define NUMHELP (sizeof(HelpMsg)/sizeof(char *))
/*******************************************************************
* Function Prototypes
********************************************************************/
void Binomial(short int NumControl), UpdateStatus(void);
void BezierToScreen(short int, short int, short int *, short int *);
short int GetInput(void), Check8087(void);
void Initialize(void), DrawCurve(short int), DrawCross(short int);
void WToVConst(void), GetHelp(void);
void main()
{
short int i, cc;
Initialize();
WToVConst();
Binomial(NumCP);
BezierToScreen(TDivisions, NumCP, Xv, Yv);
DrawCurve(ON);
UpdateStatus();
while ((cc = GetInput()) != ESC)
switch (cc)
{
case F1 : GetHelp();
break;
case F2 : DrawCurve(OFF);
Refresh = !Refresh;
DrawCurve(ON);
UpdateStatus();
break;
case LEFT : if (XCp[CurCP]-CPDx > XCpMin)
{
DrawCurve(OFF);
XCp[CurCP] -= CPDx;
BezierToScreen(TDivisions, NumCP, Xv, Yv);
DrawCurve(ON);
}
break;
case RIGHT : if (XCp[CurCP]+CPDx < XCpMax)
{
DrawCurve(OFF);
XCp[CurCP] += CPDx;
BezierToScreen(TDivisions, NumCP, Xv, Yv);
DrawCurve(ON);
}
break;
case DOWN : if (YCp[CurCP]-CPDy > YCpMin)
{
DrawCurve(OFF);
YCp[CurCP] -= CPDy;
BezierToScreen(TDivisions, NumCP, Xv, Yv);
DrawCurve(ON);
}
break;
case UP : if (YCp[CurCP]+CPDy < YCpMax)
{
DrawCurve(OFF);
YCp[CurCP] += CPDy;
BezierToScreen(TDivisions, NumCP, Xv, Yv);
DrawCurve(ON);
}
break;
case PGUP : if (CurCP+1 < NumCP)
{
DrawCross(BgdColor);
CurCP++;
DrawCurve(ON);
DrawCross(CrossColor);
UpdateStatus();
}
break;
case PGDN : if (CurCP-1 > -1)
{
DrawCross(BgdColor);
CurCP--;
DrawCurve(ON);
DrawCross(CrossColor);
UpdateStatus();
}
break;
case HOME : if (TDivisions+5 < MAXTDivisions)
{
DrawCurve(OFF);
TDivisions += 5;
BezierToScreen(TDivisions, NumCP, Xv, Yv);
DrawCurve(ON);
UpdateStatus();
}
break;
case END : if (TDivisions-5 >= MINTDivisions)
{
DrawCurve(OFF);
TDivisions -= 5;
BezierToScreen(TDivisions, NumCP, Xv, Yv);
DrawCurve(ON);
UpdateStatus();
}
break;
case INS : if (NumCP+1 < MAXCP && CurCP+1 != NumCP)
{
DrawCurve(OFF);
for (i = NumCP++; i > CurCP; i--)
{
XCp[i] = XCp[i-1];
YCp[i] = YCp[i-1];
}
Binomial(NumCP);
BezierToScreen(TDivisions, NumCP, Xv, Yv);
DrawCurve(ON);
UpdateStatus();
}
break;
case DEL : if (NumCP > 2)
{
DrawCurve(OFF);
for (i = CurCP, NumCP--; i < NumCP; i++)
{
XCp[i] = XCp[i+1];
YCp[i] = YCp[i+1];
}
if (CurCP == NumCP)
CurCP--;
Binomial(NumCP);
BezierToScreen(TDivisions, NumCP, Xv, Yv);
DrawCurve(ON);
UpdateStatus();
}
break;
}
closegraph();
}
void Initialize()
{
int Gdriver, Gmode;
if (Check8087() == 0)
{
printf("\nNo Arithmetic Coprocessor Present.");
exit(1);
}
detectgraph(&Gdriver, &Gmode);
if (Gdriver < 0)
{
printf("\nError : No graphics hardware detected.");
exit(1);
}
if (Gmode == CGAHI)
Gmode = CGAC0;
initgraph(&Gdriver, &Gmode, "");
XvpMin = 5;
YvpMin = 5;
XvpMax = getmaxx()-5;
YvpMax = getmaxy()-3-textheight("A");
XsMax = getmaxx();
YsMax = getmaxy();
if (Gdriver == CGA)
{
BgdColor = 0;
CurveColor = CGA_GREEN;
LineColor = CGA_BROWN;
CrossColor = CGA_RED;
}
else
{
BgdColor = BLACK;
CurveColor = WHITE;
LineColor = YELLOW;
Cro